package com.mux.comms;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.ConverterFacade;
import org.apache.commons.beanutils.converters.DateConverter;

/**
 * 
* @ClassName: CommonUtils 
* @Description:  
* @author chen chengxue 
* @date 2017年6月16日 上午11:19:40 
*
 */
public class CommonUtils {
	
	
	/**
	 * apache 
	 * @Description: bean 转  map ,日期类型会有问题
	 * @author chen chengxue
	 * @date 2017年6月16日 上午10:32:57
	 *
	 * @return
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public Map<String,String> beanToMap(Object bean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		return BeanUtils.describe(bean);
	}
	
	
	/**
	 * @Description:  bean 转 map，堆日期格式特殊处理
	 * @author chen chengxue
	 * @date 2017年6月16日 下午1:59:55
	 *
	 * @param bean
	 * @param pattern  ("yyyy-MM-dd");
	 * @return
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public Map<String,String> beanToMapDate(Object bean,String pattern) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		
		BeanUtilsBean beanUtilsBean = new BeanUtilsBean(new ExtConvertUtilsBean());
		DateConverter converter = new DateConverter();
		converter.setPattern(pattern);
		beanUtilsBean.getConvertUtils().register(new ConverterFacade(converter), java.util.Date.class);
		return beanUtilsBean.describe(bean);
	}
	
	/**
	 * apache Commons-BeanUtils
	 * @Description: map 转 bean
	 * @author chen chengxue
	 * @date 2017年6月16日 上午11:11:09
	 *
	 * @param bean
	 * @param map
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public Object mapToBean(Object bean,Map<String,Object> map) throws IllegalAccessException, InvocationTargetException{
		 BeanUtils.populate(bean,map);
		 return bean;
	}
	
	

}

/***
 * 
* @ClassName: ExtConvertUtilsBean 
* @Description: bean 转  map 时，会有问题，指定转换器 
* @author chen chengxue 
* @date 2017年6月16日 下午1:57:13 
*
 */
class ExtConvertUtilsBean  extends ConvertUtilsBean{
	@Override
	public String convert(Object value) {
		  if (value == null) {
	           return null;
	       } else if (value.getClass().isArray()) {
	           if (Array.getLength(value) < 1) {
	               return (null);
	           }
	           value = Array.get(value, 0);
	           if (value == null) {
	               return null;
	           } else {
	               Converter converter = lookup(String.class);
	               return (converter.convert(String.class, value));
	           }
	       } else {
	         Converter converter = null;
	         if(value instanceof java.util.Date ){
	         converter = lookup(java.util.Date.class);
	         }else{
	         converter = lookup(String.class);
	         }
	           return (converter.convert(String.class, value));
	       }

	}
}